import pymysql
pymysql.install_as_MySQLdb()

import django
from django.conf import settings
import os
from config.config import get_config

# 配置Django设置
config = get_config()

if not settings.configured:
    settings.configure(
        DATABASES = {
            'default': config['DATABASE']
        },
        INSTALLED_APPS = [
            'django.contrib.contenttypes',
            '__main__',
        ]
    )
    django.setup()

# 现在可以使用Django ORM
from django.db import models

# 定义模型
class TeachStudent(models.Model):
    class Meta:
        app_label = 'student_app'
        managed = False
        db_table = 'teach_student'
    
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, null=True)
    teach_course_id = models.CharField(max_length=100, null=True)
    student_no = models.CharField(max_length=100, null=True)
    class_no = models.CharField(max_length=100, null=True)
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    update_time = models.DateTimeField(auto_now=True, null=True)

    def __str__(self):
        return f"{self.name} ({self.student_no})"

# 查询数据
students = TeachStudent.objects.get(id=1)
# print(list(students.values()))

# 导出模型供其他模块使用
__all__ = ['TeachStudent']


